import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

import { DictionaryManageService } from "../dictionary-manage.service";
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
    selector: 'app-dictionary-manage-list',
    templateUrl: './dictionary-manage-list.component.html',
    styleUrls: ['./dictionary-manage-list.component.scss']
})
export class DictionaryManageListComponent implements OnInit {
    private deleteConfirmModal: NzModalRef;
    public listData = <Object>[];
    public listCount: Number;
    public isListOkLoading = <Boolean>false;
    public isListVisible = <Boolean>false;
    public listBlurry = <string>"";
    public listName = <string>"";
    public listRemark = <string>"";
    public listModalTips = <string>"新增字典";
    public listValidateForm: FormGroup;
    public listPage = <Number>1;
    pageSize : number = 10;
    bornRole = {}

    modalDomBox : any

    private listSelectedId = <Number>-1;

    constructor(
        private dictionaryManageService: DictionaryManageService,
        private fb: FormBuilder,
        private modal: NzModalService,
        private commonService : HCommonService
    ) { }

    ngOnInit() {
        this.modalDomBox = document.querySelector("#modal_box_1");
        this.bornRole = this.commonService.getRoles("dict");

        this.getList();
        this.listValidateForm = this.fb.group({
            listName: [null, [Validators.required]],
            listRemark: [null, [Validators.required]]
        })
    }

    handleShowDeleteModal(id): void {
        this.deleteConfirmModal = this.modal.confirm({
            nzTitle: '删除提示',
            nzContent: '确定要删除这条数据吗？',
            nzOnOk: () =>
                new Promise((resolve, reject) => {
                    this.listSelectedId = id;
                    this.listDelete();
                }).catch(() => console.log('删除失败')),
            nzOnCancel: ()=>{
                this.listSelectedId = -1;
            }
        });
    }

    handleShowListModal(id, data?): void {
        this.isListVisible = true;
        this.listSelectedId = id;
        if (data) {
            this.listName = data.name;
            this.listRemark = data.remark;
            this.listModalTips = "编辑字典"
        } else {
            this.listValidateForm.reset();
            this.listModalTips = "新增字典"
        }
    }
    handleListCancel(): void {
        this.isListVisible = false;
        this.isListOkLoading = false;
    }
    handleListSubmit(): void {
        if (this.listSelectedId == -1) {
            this.listAdd();
        } else {
            this.listEdit();
        }
    }
    handleListSearch() {
        this.listPage = 1;
        this.getList()
    }
    // zxh 修改成 点击选中行
    selectDict:string = ''
    handleViewDetail(name, id) {
        this.selectDict = id
        this.dictionaryManageService.detailParantName = name
        this.dictionaryManageService.detailParentId = Number(id)
    }
    listPageChange(ev) {
        this.listPage = ev;
        this.getList();
    }
    hotSearch(){
        this.listPage = 1;
        this.getList();
    }
    getList() {
        let _dataJson = {
            blurry: this.listBlurry,
            page: this.listPage,
            size: this.pageSize
        }
        this.dictionaryManageService.getList(_dataJson).subscribe(res => {
            this.listData = res.content;
            this.listCount = res.totalElements
        });
    }
    listAdd() {
        for (const i in this.listValidateForm.controls) {
            this.listValidateForm.controls[i].markAsDirty();
            this.listValidateForm.controls[i].updateValueAndValidity();
        }

        if (this.listValidateForm.status == "VALID") {
            this.isListOkLoading = true;
            let _value = this.listValidateForm.value;
            let _dataJson = {
                name: _value.listName,
                remark: _value.listRemark
            }
            this.dictionaryManageService.listAdd(_dataJson).subscribe(res => {
                this.getList();
                this.handleListCancel();
            }, ()=>{
                this.isListOkLoading = false;
            })
        }
    }
    listDelete() {
        if (this.listSelectedId == -1) return;
        this.dictionaryManageService.listDelete(this.listSelectedId).subscribe(res => {
            this.getList();
            this.deleteConfirmModal.close();
        })
    }
    listEdit() {
        if (this.listSelectedId == -1) return;
        for (const i in this.listValidateForm.controls) {
            this.listValidateForm.controls[i].markAsDirty();
            this.listValidateForm.controls[i].updateValueAndValidity();
        }
        if (this.listValidateForm.status == "VALID") {
            let _value = this.listValidateForm.value;
            let _dataJson = {
                id: this.listSelectedId,
                name: _value.listName,
                remark: _value.listRemark
            }
            this.dictionaryManageService.listEdit(_dataJson).subscribe(res => {
                this.getList();
                this.handleListCancel();
            }, ()=>{
                this.isListOkLoading = false;
            })
        }
    }

}
